package elementos;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;

public class Brick implements Movible{
    private float x,y,width,height,dx=0;
    private boolean moving=false;

    public void setMoving(boolean moving) {
        this.moving = moving;
    } 

    public float getX() {
        return x;
    }

    public float getY() {
        return y;
    }

    public float getWidth() {
        return width;
    }

    public float getHeight() {
        return height;
    }
    
    public Brick(float x, float y, float width, float height){
        this.x=x;
        this.y=y;
        this.width=width;
        this.height=height;
    }
    
    public void render(Graphics g) {
        Graphics2D g2 = (Graphics2D)g;
        g2.setBackground(Color.white);
        Color c = g2.getColor();
        g2.setColor(Color.BLUE);
        g2.fillRect((int)x, (int)y,(int) width,(int) height);
        g2.setColor(c);
    }

    public void add(Drawable d) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void remove(Drawable d) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public Drawable getChild(int i) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public Drawable collision(Drawable d) {
        if(d instanceof Ball){
            Ball b = (Ball) d;
            float x = b.getX() - b.getRadio();
            float xf = b.getX() + b.getRadio();
            float y = b.getY() - b.getRadio();
            float yf = b.getY() + b.getRadio();
            float xc = b.getX(), yc = b.getY();
            //ball situated on right side 3 posible collisions
            if (x < this.x + width && x > this.x) {
                //ball situated under the brick 2 posible collisions
                if (y < this.y + width && y > this.y) {
                    //right collision
                    if (yc > this.y && yc < this.y + width) {
                        b.invertX();
                    } else {
                        //bottom collision
                        b.invertY();
                    }
                    b.move();
                    dx=b.getDX();
                    setMoving(true);
                    return b;
                }
                //ball situated over the brick 2 posible collisions
                if (yf > this.y && yf <this.y + width) {
                    //right collision
                    if (yc > this.y && yc < this.y + width) {
                        b.invertX();
                    } else {
                        //top collision
                        b.invertY();
                    }
                    b.move();
                    dx=b.getDX();
                    setMoving(true);
                    return b;
                }
            }
            //ball situated on left side 3 posible collisions
            if (xf > this.x && xf < this.x + width) {
                //ball situated under the brick 2 posible collitions
                if (y < this.y + width && y > this.y) {
                    //left collision
                    if (yc > this.y && yc < this.y + width) {
                        b.invertX();
                    } else {
                        //bottom collision
                        b.invertY();
                    }
                    b.move();
                    dx=b.getDX();
                    setMoving(true);
                    return b;
                }
                //ball situated over the brick 2 posible collisions
                if (yf > this.y && yf < this.y + width) {
                    //left collision
                    if (yc > this.y && yc < this.y + width) {
                        b.invertX();
                    } else {
                        //top collision
                        b.invertY();
                        
                    }
                    b.move();
                    setMoving(true);
                    dx=b.getDX();
                    return b;
                }
            }
            return null;
        }
        return null;
    }    

    @Override
    public void moveObject() {
        if(moving)this.x+=dx;
    }
}
